package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @Override
    public PageResult page(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> page = dishMapper.page(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void status(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dish.setId(id);

        dishMapper.update(dish);
    }

    @Transactional
    public void delete(String ids) {

        String[] idArr = ids.split(",");
        //起售中的菜品不允许删除
        for (String s : idArr) {
            Dish dish = dishMapper.selectById(Long.parseLong(s));
            if (dish.getStatus() == 1) {
                throw new DeletionNotAllowedException("起售中的菜品不允许删除");
            }

        }
        dishMapper.delete(idArr);
        //删除菜品口味
        dishFlavorMapper.deleteByDishIds(idArr);
    }

   @Transactional
    public void add(DishDTO dishDTO) {
        //先插入菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.DISABLE);
        dish.setCreateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
     dishMapper.insert(dish);
        //获取刚插入的菜品id
        Long id = dish.getId();

        //再插入菜品口味
       dishFlavorMapper.insertBatch(dishDTO.getFlavors(), id);
    }

    @Override
    public DishVO getById(Long id) {
        //查询菜品
        Dish dish = dishMapper.selectById(id);
        //查询菜品口味
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);

        //封装返回值
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

   @Transactional
    public void update(DishDTO dishDTO) {
        //先更新菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.update(dish);

        //再更新菜品口味
        dishFlavorMapper.deleteByDishIds(new String[]{dish.getId().toString()});
        dishFlavorMapper.insertBatch(dishDTO.getFlavors(), dish.getId());
    }

    @Override
    public List<Dish> listByCategoryId(Integer categoryId) {
        return dishMapper.selectByCategoryId(Long.valueOf(categoryId));
    }

    @Override
    public List<DishVO> list(Long categoryId) {
        ArrayList<DishVO> dishVOS = new ArrayList<>();
        //查询菜品
        List<Dish> dishList = dishMapper.getDishAndCategoryNameByCategoryId(categoryId);

        //查询菜品口味
        for (Dish dish : dishList) {
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(dish.getId());
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            dishVO.setFlavors(flavors);
            dishVOS.add(dishVO);
        }
        return dishVOS;
    }
}
